package com.bff.gaia.client.cli;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.concurrent.EventExecutorGroup;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class ClientHandler extends ChannelInboundHandlerAdapter {
	public final String[] strings;
	private int byteRead;
	private volatile int start = 0;
	private volatile int lastLength = 0;
	public RandomAccessFile randomAccessFile;
	private FileUploadFile fileUploadFile;

	public ClientHandler(String[] str,FileUploadFile f) {
		this.strings=str;
		this.fileUploadFile=f;
	}
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		String args=this.fileUploadFile.getInput();
		System.out.println("打印客户端的参数");
		System.out.println(args);
		System.out.println("-------------------client at run options channelActive..");
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < strings.length; i++){
			sb.append(strings[i]);
		}
		try {
			randomAccessFile = new RandomAccessFile(fileUploadFile.getFile(),
				"r");
			randomAccessFile.seek(fileUploadFile.getStarPos());
			// lastLength = (int) randomAccessFile.length() / 10;
			lastLength = 1024 * 5;
			byte[] bytes = new byte[lastLength];
			if ((byteRead = randomAccessFile.read(bytes)) != -1) {
				fileUploadFile.setEndPos(byteRead);
				fileUploadFile.setBytes(bytes);
				ctx.writeAndFlush(fileUploadFile);
			} else {

			}
			System.out.println("channelActive()文件已经传输结束 "+byteRead);
			ctx.writeAndFlush(-1);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException i) {
			i.printStackTrace();
		}
	/*	String s = sb.toString();
		System.out.println("待发送的字符串为："+s+";	待发送的字符串长度为："+s.length());
		byte[] req = s.getBytes();
		ByteBuf firstMessage = Unpooled.buffer(req.length);
		firstMessage.writeBytes(req);
		ctx.writeAndFlush(firstMessage);*/
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		if (msg instanceof Integer) {
			start = (Integer) msg;
			if (start != -1) {
				randomAccessFile = new RandomAccessFile(
					fileUploadFile.getFile(), "r");
				randomAccessFile.seek(start);
				System.out.println("长度：" + (randomAccessFile.length() - start));
				int a = (int) (randomAccessFile.length() - start);
				int b = (int) (randomAccessFile.length() / 1024*2);
				if (a < lastLength) {
					lastLength = a;
				}
				System.out.println("文件长度：" + (randomAccessFile.length())+",start:"+start+",a:"+a+",b:"+b+",lastLength:"+lastLength);
				byte[] bytes = new byte[lastLength];
				// log.info("-----------------------------" + bytes.length);
				if ((byteRead = randomAccessFile.read(bytes)) != -1
					&& (randomAccessFile.length() - start) > 0) {
					// log.info("byte 长度：" + bytes.length);
					fileUploadFile.setEndPos(byteRead);
					fileUploadFile.setBytes(bytes);
					try {
						ctx.writeAndFlush(fileUploadFile);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					randomAccessFile.close();
					//ctx.close();
					System.out.println("文件已经读完channelRead()--------" + byteRead);
				}
			}
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		super.exceptionCaught(ctx, cause);
	}
}